/// <reference types="node" />
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  // 尝试从环境变量获取主机和端口，但我们将被命令行参数覆盖
  const host = process.env.VITE_HOST || 'localhost';
  const port = parseInt(process.env.VITE_PORT || '3000');
  const backendHost = process.env.VITE_BACKEND_HOST || 'localhost';
  const backendPort = process.env.VITE_BACKEND_PORT || '8000';

  console.log(`配置服务器: 主机=${host}, 端口=${port}, 后端主机=${backendHost}, 后端端口=${backendPort}`);

  return {
    plugins: [react()],
    resolve: {
      alias: {
        '@': resolve(__dirname, './src'),
      },
    },
    server: {
      // 这些将被命令行参数覆盖，但我们仍然设置它们作为默认值
      host,
      port,
      proxy: {
        '/api': {
          target: `http://${backendHost}:${backendPort}`,
          changeOrigin: true,
        },
      },
    },
    define: {
      'import.meta.env.VITE_BACKEND_HOST': JSON.stringify(backendHost),
      'import.meta.env.VITE_BACKEND_PORT': JSON.stringify(backendPort),
    }
  }
}) 